<template>
    <base-breadcrumb>
    <a-spin :spinning="tableLoading">
      <a-card>
        <titleName title="基本信息"></titleName>
        <a-form-model
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
          :wrapperCol="{ lg: { span: 16 }, sm: { span: 16 } }"
        >
          <a-row :gutter="24">

              <a-col span="16">
              <a-form-model-item label="所属单位" prop="projectId">
                <org-tree-select
                                 v-model="form.projectId"
                                 isAutoSelectFirst checkStrictly
                                 disabled
                                 @select="selectOrg"></org-tree-select>
                                   <!-- :disabled="pageType == 'looks'" -->
              </a-form-model-item>
            </a-col>
            <a-col :md="8" :sm="8">
              <a-form-model-item label="名称" prop="name">
                <a-input  v-model="form.name" disabled />
              </a-form-model-item>
            </a-col>
            <a-col :md="8" :sm="8">
              <a-form-model-item   label="总包施工图预算收入金额(含税)（元）：" prop="totalConstructionAmount">
                <a-input-number :precision="2"  v-model="form.totalConstructionAmount" :disabled="pageType == 'looks'" @change="handlechange" />
              </a-form-model-item>
            </a-col>
            <a-col :md="8" :sm="8">
              <a-form-model-item label="分包分供预计支出金额(含税)（元）：" prop="subSupplyExpenditure">
                <a-input-number  v-model="form.subSupplyExpenditure" :disabled="pageType == 'looks'" @change="handlechange" />
              </a-form-model-item>
            </a-col>
            <a-col :md="8" :sm="8">
              <a-form-model-item label="预计效益率（%）：" prop="benefitRate">
                <a-input-number  v-model="form.benefitRate" disabled />
              </a-form-model-item>
            </a-col>
            <a-col :md="8" :sm="8">
              <a-form-model-item label="填报人" prop="informant">
                <a-input  v-model="form.informant" disabled />
              </a-form-model-item>
            </a-col>
            <a-col :md="8" :sm="8">
              <a-form-model-item label="填报时间：" prop="informantDate">
                <a-date-picker disabled v-model="form.informantDate" @change="(date, dateString) => getTime(date, dateString, 'form', 'informantDate')" />
                <!-- <a-input  v-model="form.informantTime" :disabled="pageType == 'looks'" /> -->
              </a-form-model-item>
            </a-col>
            <a-col :md="24" :sm="24">
                  <a-form-model-item label="备注" prop="remark" :labelCol="{ lg: { span: 5 }, sm: { span: 5 } }" :wrapperCol="{ lg: { span: 18 }, sm: { span: 17 } }">
                    <!-- <BaseTextarea :auto-size="{ minRows: 3, maxRows: 5 }" v-model="form.participants" /> -->
                    <a-input v-model="form.remark" type="textarea"  :disabled="pageType == 'looks'" />
                  </a-form-model-item>
              </a-col>
            <a-col  :md="24" :sm="24">
              <a-form-model-item label="附件上传">
                <upload-list
                  :businessCode="'BM19'"
                  :disabled="pageType == 'looks'"
                  :uploadArrys.sync="form.fileInfoV0List"
                  @fileArrysData="handleFileUploaded"
                  multiple
                ></upload-list>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </a-card>
      <div class="tool-btns page-btn-right-top">
  
        <a-button v-if="pageType != 'looks'" type="primary"  :loading="loading.SUBMITTED" @click="handleSave('DRAFT')"
          >保存草稿</a-button
        >
        <a-button type="primary" v-if="pageType != 'looks'" :loading="loading.SUBMITTED" @click="handleSave('SUBMITTED')"
          >提交</a-button
        >
        <a-button  @click="handleClose">关闭</a-button>
      </div>
      </a-spin>
      <selectApprover :businessId="businessId"  :processConfig="setProcessConfig"></selectApprover>
    </base-breadcrumb>
  </template>
  <script>
  
  import { STable } from '@/components'
  import UploadList from '@/components/UploadList'
  import { LookList, addList } from '@/api/businessAffairs/benefitAnalysis'
  import moment from 'moment'
  export default {
    name: 'a' + Date.now(),
    components: {
      STable,
      UploadList,
    },
    data() {
      return {
        tableLoading: false,
        loading: {
          DELETE: false,
          DRAFT: false,
          SUBMITTED: false,
          REJECTED: false
        },
        form: {
          projectId: '',
          name:'',
          totalConstructionAmount:'',
          subSupplyExpenditure:'',
          benefitRate:'',
          informant:'',
          informantDate:'',
          remark:'',
          fileIdList: []
        },
        rules: {
          totalConstructionAmount:[{ required: true, message: '请输入' }],
          subSupplyExpenditure:[{ required: true, message: '请输入' }],
        },
        id: '',
        pageType: '',
     
        //获取流程配置值
        processConfig: null,
        //点击提交发送流程配置值
        setProcessConfig: null,
        businessId: null
      }
    },
  
  
 async created() {
   this.pageType = this.$route.query.pageType
   this.getLists()
   this.processConfig = await this.$getConfigFn('comm_master_work_plan')
  },
    methods: {
      getTime(date, dateString, type, name) {
        this[type][name] = dateString
        console.log(this[type][name])
      },
      // 获取详情
      getLists(){
        LookList({id: this.$route.query.id}).then((res) => {
            if (res.code == 200) {
              const data = res.data
              this.form = data
              this.form.fileInfoV0List = res.data.reviewFileInfoVOList
              if( this.pageType !== 'looks') {
                this.form.informant = this.$userInfo().name
                this.form.informantDate = moment().format("YYYY-MM-DD")
          }
            }
          })
      },
  
      handleFileUploaded(list) {
        console.log(list)
        this.form.fileIdList = []
        list.forEach(item => {
          this.form.fileIdList.push(item.commonId)
        })
      },
    
       //获取项目id
      selectOrg(val) {
          if (val && val.length) {
            this.form.branchComId = val[val.length - 2].orgId
          }
      },
    
      handleClose() {
          this.$multiTab.close(this.$route.fullpath)
          // this.$router.replace("/businessAffairs/businessOrganization/designPlanning")
      },
      handleSave(auditStatus) {
      this.form.auditStatus = auditStatus
      this.form.id = this.$route.query.id
      this.form.informantUserId = this.$userInfo().id
      const sumbit =() => {
        this.loading.SUBMITTED=true
        addList(this.form).then(res =>{
          this.loading.SUBMITTED=false
          if(res.code === 200){
            this.$message.success('成功')
            this.handleClose()
          }else{
            this.$message.warning('失败')
          }
        })
      }
      if(auditStatus == 'DRAFT'){
        sumbit()
      }else{
        this.$refs.ruleForm.validate((valid) => {
            if (valid) {
              sumbit()
            }else {
            this.$message.warning("请填写完所有必填项信息")
            return false
          }
        })
        
      
      }
    },
    handlechange(){
      if(this.form.totalConstructionAmount && this.form.subSupplyExpenditure){
        this.form.benefitRate=((this.form.totalConstructionAmount - this.form.subSupplyExpenditure)/this.form.totalConstructionAmount*100).toFixed(2)
      }
    }
    }
  }
  </script>
  <style lang="less" scoped>
  @deep: ~'>>>';
  .tool-btns {
    display: flex;
    justify-content: flex-end;
    @{deep} .ant-btn + .ant-btn {
      margin-left: 12px;
    }
  }
  </style>
  